<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="价格区间始" prop="changePriceStart">
      <el-input v-model="dataForm.changePriceStart" placeholder="价格区间始"></el-input>
    </el-form-item>
    <el-form-item label="价格区间止" prop="changePriceEnd">
      <el-input v-model="dataForm.changePriceEnd" placeholder="价格区间止"></el-input>
    </el-form-item>
    <el-form-item label="加价百分比" prop="changePricePercent">
      <el-input v-model="dataForm.changePricePercent" placeholder="加价百分比"></el-input>
    </el-form-item>
    <el-form-item label="加价额" prop="changePriceAmount">
      <el-input v-model="dataForm.changePriceAmount" placeholder="加价额"></el-input>
    </el-form-item>
    <el-form-item label="最高价" prop="highestPrice">
      <el-input v-model="dataForm.highestPrice" placeholder="最高价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dtId: '',
          hotelId: '',
          storeId: null,
          changePriceType: '',
          changePriceStart: '',
          changePriceEnd: '',
          changePricePercent: '',
          changePriceAmount: '',
          highestPrice: '',
          fixedPrice: ''
        },
        dataRule: {
          dtId: [
            { required: true, message: '分销售商编码不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '店铺编码不能为空', trigger: 'blur' }
            ],
          hotelId: [
            { required: true, message: '酒店ID不能为空', trigger: 'blur' }
          ],
          changePriceType: [
            { required: true, message: '加价类型不能为空', trigger: 'blur' }
          ],
          changePriceStart: [
            { required: true, message: '价格区间始不能为空', trigger: 'blur' }
          ],
          changePriceEnd: [
            { required: true, message: '价格区间止不能为空', trigger: 'blur' }
          ],
          changePricePercent: [
            { required: true, message: '加价百分比不能为空', trigger: 'blur' }
          ],
          changePriceAmount: [
            { required: true, message: '加价额不能为空', trigger: 'blur' }
          ],
          highestPrice: [
            { required: true, message: '最高价不能为空', trigger: 'blur' }
          ],
          fixedPrice: [
            { required: true, message: '固定额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,hotelCode,storeId) {
        this.dataForm.id = id || 0
        this.dataForm.hotelId = hotelCode
        this.dataForm.storeId = storeId
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/op/htlprice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                // this.dataForm.dtId = data.htlPrice.dtId
                this.dataForm.hotelId = data.htlPrice.hotelId
                this.dataForm.changePriceStart = data.htlPrice.changePriceStart
                this.dataForm.changePriceEnd = data.htlPrice.changePriceEnd
                this.dataForm.changePricePercent = data.htlPrice.changePricePercent
                this.dataForm.changePriceAmount = data.htlPrice.changePriceAmount
                this.dataForm.highestPrice = data.htlPrice.highestPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/op/htlprice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dtId': this.dataForm.dtId,
                'storeId': this.dataForm.storeId,
                'hotelId': this.dataForm.hotelId,
                'changePriceType': this.dataForm.changePriceType,
                'changePriceStart': this.dataForm.changePriceStart,
                'changePriceEnd': this.dataForm.changePriceEnd,
                'changePricePercent': this.dataForm.changePricePercent,
                'changePriceAmount': this.dataForm.changePriceAmount,
                'highestPrice': this.dataForm.highestPrice,
                'fixedPrice': this.dataForm.fixedPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
